public class OverridePESampleNew
{
    public String IdSelect{get;set;}
    public String Prefix = PE_Sample_Management__c.sObjectType.getDescribe().getKeyPrefix();  
    public String msg{get;set;}
    public PE_Sample_Management__c PESM{
         get{
             if(PESM== null)
                 PESM= new PE_Sample_Management__c();
             return PESM;
         }
         set;}
             
         
    public List<TableItemControl> TableItemsUp{
         get{
             if(TableItemsUp == null)
                 TableItemsUp = new List<TableItemControl>();
             return TableItemsUp;
         }
        set;}
        
    public List<TableItemControl> TableItemsDown{         
        get{
             if(TableItemsDown== null)
                 TableItemsDown= new List<TableItemControl>();
             return TableItemsDown;
         }
        set;}    
    
    public String UrlId
    {
        get{        
            return System.currentPageReference().getParameters().get('id');
        }  
        set;
    }  
    public Boolean TestServer;
     
    public OverridePESampleNew(ApexPages.StandardController controller){
        this.controller=controller;
       
        TestServer = true;
        if(Date.Today().month() == 12)
        {
            msg = '创建日期已过！';
        }
        TestServer__c ex=TestServer__c.getValues('TestOrNot');
        TestServer = ex.Test__c;
   
        PESM.Year__c = String.valueof(Date.today().Year() + 1);
                   
        TableItemControl T1 = new TableItemControl();
        T1.Str = '总预算';
        T1.Str1 = '%';
        T1.PE.Total_Budget_weighting__c = 100;
        TableItemControl T2 = new TableItemControl();
        T2.Str = '保留在总部';
        T2.Str1 = '%';
        TableItemControl T3 = new TableItemControl();
        T3.Str = '分配到大区';  
        T3.Str1 = '%'; 
        TableItemsUp.add(T1);
        TableItemsUp.add(T2);
        TableItemsUp.add(T3);
                
        
        // get last year total in market sales value and conversion value
       // List<AggregateResult> TotalLastYearInMarketSales = [Select sum(InMarketSaleskg__c)TotalIn,Region__c region  From InMarketSales__c Where InMarketSaleskg__c > 0 and Year__c =: integer.valueof(PESM.Year__c)-1 group by Region__c];
        List<InMarketSales__c> TotalLastYearInMarketSales = [Select InMarketSaleskg__c,Region__c From InMarketSales__c Where InMarketSaleskg__c > 0 and Year__c =: integer.valueof(PESM.Year__c) -1 ];
        Decimal TotalLastYearInMarketSalesValue = 0;
        Decimal EOCLastYearInMarketSalesValue = 0;
        Decimal SOCLastYearInMarketSalesValue = 0;
        Decimal NOCLastYearInMarketSalesValue = 0;
        Decimal WOCLastYearInMarketSalesValue = 0;
        for(InMarketSales__c t : TotalLastYearInMarketSales )
        {
            if(t.Region__c == '华东区')
                EOCLastYearInMarketSalesValue += (Decimal)t.InMarketSaleskg__c;
            if(t.Region__c == '华南区')
                SOCLastYearInMarketSalesValue += (Decimal)t.InMarketSaleskg__c;
            if(t.Region__c == '华北区')
                NOCLastYearInMarketSalesValue += (Decimal)t.InMarketSaleskg__c;
            if(t.Region__c == '华西区')
                WOCLastYearInMarketSalesValue += (Decimal)t.InMarketSaleskg__c;                                                
        }
        TotalLastYearInMarketSalesValue = EOCLastYearInMarketSalesValue + SOCLastYearInMarketSalesValue + NOCLastYearInMarketSalesValue + WOCLastYearInMarketSalesValue;
        
        
        List<InMarketSales__c> TotalThisYearInMarketSales = [Select InMarketSalesForcast__c,Region__c From InMarketSales__c Where InMarketSalesForcast__c > 0 and Year__c =: integer.valueof(PESM.Year__c)];
        Decimal TotalThisYearInMarketSalesValue = 0;
        Decimal EOCThisYearInMarketSalesValue = 0;
        Decimal SOCThisYearInMarketSalesValue = 0;
        Decimal NOCThisYearInMarketSalesValue = 0;
        Decimal WOCThisYearInMarketSalesValue = 0;
        for(InMarketSales__c t : TotalThisYearInMarketSales )
        {
            if(t.Region__c == '华东区'){
                EOCThisYearInMarketSalesValue += (Decimal)t.InMarketSalesForcast__c;

            }    
            if(t.Region__c == '华南区')
                SOCThisYearInMarketSalesValue += (Decimal)t.InMarketSalesForcast__c;
            if(t.Region__c == '华北区')
                NOCThisYearInMarketSalesValue += (Decimal)t.InMarketSalesForcast__c;
            if(t.Region__c == '华西区')
                WOCThisYearInMarketSalesValue += (Decimal)t.InMarketSalesForcast__c;                                                
        }
        TotalThisYearInMarketSalesValue = EOCThisYearInMarketSalesValue + SOCThisYearInMarketSalesValue + NOCThisYearInMarketSalesValue + WOCThisYearInMarketSalesValue ;
             

        List<AggregateResult> EOCLastYearPEConversion = [Select sum(PE_conversion_achieve__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: String.valueof(integer.valueof(PESM.Year__c)-1) and region__c =: '华东区'];
        List<AggregateResult> SOCLastYearPEConversion = [Select sum(PE_conversion_achieve__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: String.valueof(integer.valueof(PESM.Year__c)-1) and region__c =: '华南区'];
        List<AggregateResult> NOCLastYearPEConversion = [Select sum(PE_conversion_achieve__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: String.valueof(integer.valueof(PESM.Year__c)-1) and region__c =: '华北区'];
        List<AggregateResult> WOCLastYearPEConversion = [Select sum(PE_conversion_achieve__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: String.valueof(integer.valueof(PESM.Year__c)-1) and region__c =: '华西区'];
        Decimal TotalLastYearPEConversionValue = 0;
        Decimal EOCLastYearPEConversionValue = 0;
        Decimal SOCLastYearPEConversionValue = 0;
        Decimal NOCLastYearPEConversionValue = 0;
        Decimal WOCLastYearPEConversionValue = 0;

            if((Double)EOCLastYearPEConversion[0].get('TotalIn') > 0){
                EOCLastYearPEConversionValue = (Decimal)EOCLastYearPEConversion[0].get('TotalIn');
            }                    
            if((Double)SOCLastYearPEConversion[0].get('TotalIn') > 0)
                SOCLastYearPEConversionValue = (Decimal)SOCLastYearPEConversion[0].get('TotalIn');
            if((Double)NOCLastYearPEConversion[0].get('TotalIn') > 0)
                NOCLastYearPEConversionValue = (Decimal)NOCLastYearPEConversion[0].get('TotalIn');
            if((Double)WOCLastYearPEConversion[0].get('TotalIn') > 0)
                WOCLastYearPEConversionValue = (Decimal)WOCLastYearPEConversion[0].get('TotalIn');          
                                                     
        TotalLastYearPEConversionValue = EOCLastYearPEConversionValue + SOCLastYearPEConversionValue + NOCLastYearPEConversionValue + WOCLastYearPEConversionValue;
          
        List<AggregateResult> EOCThisYearPEConversion = [Select sum(PE_Conversion_forecast__c)TotalIn  From Achieve__c Where PE_Conversion_forecast__c > 0 and   Year__c =: PESM.Year__c and region__c =: '华东区'];
        List<AggregateResult> SOCThisYearPEConversion = [Select sum(PE_Conversion_forecast__c)TotalIn  From Achieve__c Where PE_Conversion_forecast__c > 0 and   Year__c =: PESM.Year__c and region__c =: '华南区'];
        List<AggregateResult> NOCThisYearPEConversion = [Select sum(PE_Conversion_forecast__c)TotalIn  From Achieve__c Where PE_Conversion_forecast__c > 0 and   Year__c =: PESM.Year__c and region__c =: '华北区'];
        List<AggregateResult> WOCThisYearPEConversion = [Select sum(PE_Conversion_forecast__c)TotalIn  From Achieve__c Where PE_Conversion_forecast__c > 0 and   Year__c =: PESM.Year__c and region__c =: '华西区'];
        
        Decimal TotalThisYearPEConversionValue = 0;
        Decimal EOCThisYearPEConversionValue = 0;
        Decimal SOCThisYearPEConversionValue = 0;
        Decimal NOCThisYearPEConversionValue = 0;
        Decimal WOCThisYearPEConversionValue = 0;

        if(((Double)EOCThisYearPEConversion[0].get('TotalIn') > 0)&&((Decimal)EOCThisYearPEConversion[0].get('TotalIn') != null)){
            EOCThisYearPEConversionValue = (Decimal)EOCThisYearPEConversion[0].get('TotalIn');   
                                     
        }                 
        if(((Double)SOCThisYearPEConversion[0].get('TotalIn') > 0)&&((Decimal)SOCThisYearPEConversion[0].get('TotalIn') != null))
            SOCThisYearPEConversionValue = (Decimal)SOCThisYearPEConversion[0].get('TotalIn');
        if(((Double)NOCThisYearPEConversion[0].get('TotalIn') > 0)&&((Decimal)NOCThisYearPEConversion[0].get('TotalIn') != null))
            NOCThisYearPEConversionValue = (Decimal)NOCThisYearPEConversion[0].get('TotalIn');
        if(((Double)WOCThisYearPEConversion[0].get('TotalIn') > 0)&&((Decimal)WOCThisYearPEConversion[0].get('TotalIn') != null))
            WOCThisYearPEConversionValue = (Decimal)WOCThisYearPEConversion[0].get('TotalIn');       
        
        TotalThisYearPEConversionValue = EOCThisYearPEConversionValue + SOCThisYearPEConversionValue + NOCThisYearPEConversionValue + WOCThisYearPEConversionValue;

        List<Shipment__c > TotalLastYearPEShipment = [Select KG__c,Region__c From Shipment__c Where KG__c > 0 and   Year__c =: integer.valueof(PESM.Year__c)-1 and CalcInKg__c =: true];
        Decimal TotalLastYearPEShipmentValue = 0;
        Decimal EOCLastYearPEShipmentValue = 0;
        Decimal SOCLastYearPEShipmentValue = 0;
        Decimal NOCLastYearPEShipmentValue = 0;
        Decimal WOCLastYearPEShipmentValue = 0;
        for(Shipment__c t : TotalLastYearPEShipment )
        {
            if(t.region__c == '华东区'){
                EOCLastYearPEShipmentValue += (Decimal)t.KG__c;
            }    
            if(t.region__c == '华南区')
                SOCLastYearPEShipmentValue += (Decimal)t.KG__c;
            if(t.region__c == '华北区')
                NOCLastYearPEShipmentValue += (Decimal)t.KG__c;
            if(t.region__c == '华西区')
                WOCLastYearPEShipmentValue += (Decimal)t.KG__c;                                                
        }
        TotalLastYearPEShipmentValue = EOCLastYearPEShipmentValue + SOCLastYearPEShipmentValue + NOCLastYearPEShipmentValue + WOCLastYearPEShipmentValue;         
               
        // create EOC data
        TableItemControl TE = new TableItemControl();
        TE.Str = '华东区';
        
        if(EOCLastYearPEShipmentValue > 0) 
            TE.PE.SOC_Last_year_In_market_sales_kg__c = EOCLastYearInMarketSalesValue / EOCLastYearPEShipmentValue;
        else
            TE.PE.SOC_Last_year_In_market_sales_kg__c = 0;
        if(EOCLastYearPEConversionValue > 0) 
            TE.PE.SOC_Last_year_PE_Sample_kg__c = EOCLastYearPEShipmentValue / EOCLastYearPEConversionValue ;
        else
            TE.PE.SOC_Last_year_PE_Sample_kg__c = 0;
        
        TE.PE.SOC_This_year_In_market_sales_kg__c = 0;
        TE.PE.SOC_This_year_PE_Sample_kg__c = 0;
        TE.PE.SOCInMarketValueHide__c = EOCThisYearInMarketSalesValue;
        TE.PE.SOCConversionForcast__c = EOCThisYearPEConversionValue;    

        if(TotalLastYearInMarketSalesValue > 0) 
            TE.PE.SOC_Last_year_sales_weighting__c = EOCLastYearInMarketSalesValue / TotalLastYearInMarketSalesValue * 100;
        else
            TE.PE.SOC_Last_year_sales_weighting__c = 0;
        if(TotalLastYearPEConversionValue > 0) 
            TE.PE.SOC_Last_year_conversion_weighting__c = EOCLastYearPEConversionValue / TotalLastYearPEConversionValue * 100;
        else
            TE.PE.SOC_Last_year_conversion_weighting__c = 0;
        TableItemsDown.add(TE);   
        
        TableItemControl TN = new TableItemControl();
        TN.Str = '华北区';
        if(NOCLastYearPEShipmentValue > 0) 
            TN.PE.SOC_Last_year_In_market_sales_kg__c = NOCLastYearInMarketSalesValue / NOCLastYearPEShipmentValue;
        else
            TN.PE.SOC_Last_year_In_market_sales_kg__c = 0;
        if(NOCLastYearPEConversionValue > 0) 
            TN.PE.SOC_Last_year_PE_Sample_kg__c = NOCLastYearPEShipmentValue / NOCLastYearPEConversionValue ;
        else
            TN.PE.SOC_Last_year_PE_Sample_kg__c = 0;
        TN.PE.SOC_This_year_In_market_sales_kg__c = 0;
        TN.PE.SOC_This_year_PE_Sample_kg__c = 0;
        TN.PE.SOCInMarketValueHide__c = NOCThisYearInMarketSalesValue ;
        TN.PE.SOCConversionForcast__c = NOCThisYearPEConversionValue; 
                               
        if(TotalLastYearInMarketSalesValue > 0) 
            TN.PE.SOC_Last_year_sales_weighting__c = NOCLastYearInMarketSalesValue / TotalLastYearInMarketSalesValue * 100;
        else
            TN.PE.SOC_Last_year_sales_weighting__c = 0;
        if(TotalLastYearPEConversionValue > 0) 
            TN.PE.SOC_Last_year_conversion_weighting__c = NOCLastYearPEConversionValue / TotalLastYearPEConversionValue * 100;
        else
            TN.PE.SOC_Last_year_conversion_weighting__c = 0;
        TableItemsDown.add(TN); 
                
        TableItemControl TS = new TableItemControl();
        TS.Str = '华南区';
        if(SOCLastYearPEShipmentValue > 0) 
            TS.PE.SOC_Last_year_In_market_sales_kg__c = SOCLastYearInMarketSalesValue / SOCLastYearPEShipmentValue;
        else
            TS.PE.SOC_Last_year_In_market_sales_kg__c = 0;
        if(SOCLastYearPEConversionValue > 0) 
            TS.PE.SOC_Last_year_PE_Sample_kg__c =SOCLastYearPEShipmentValue / SOCLastYearPEConversionValue ;
        else
            TS.PE.SOC_Last_year_PE_Sample_kg__c = 0;
        TS.PE.SOC_This_year_In_market_sales_kg__c = 0; 
        TS.PE.SOC_This_year_PE_Sample_kg__c = 0;
        TS.PE.SOCInMarketValueHide__c = SOCThisYearInMarketSalesValue ;
        TS.PE.SOCConversionForcast__c = SOCThisYearPEConversionValue;                    

        if(TotalLastYearInMarketSalesValue > 0) 
            TS.PE.SOC_Last_year_sales_weighting__c = SOCLastYearInMarketSalesValue / TotalLastYearInMarketSalesValue * 100;
        else
            TS.PE.SOC_Last_year_sales_weighting__c = 0;
        if(TotalLastYearPEConversionValue > 0) 
            TS.PE.SOC_Last_year_conversion_weighting__c = SOCLastYearPEConversionValue / TotalLastYearPEConversionValue * 100;
        else
            TS.PE.SOC_Last_year_conversion_weighting__c = 0;
        TableItemsDown.add(TS);  

        TableItemControl TW = new TableItemControl();
        TW.Str = '华西区';
        if(WOCLastYearPEShipmentValue > 0) 
            TW.PE.SOC_Last_year_In_market_sales_kg__c = WOCLastYearInMarketSalesValue / WOCLastYearPEShipmentValue;
        else
            TW.PE.SOC_Last_year_In_market_sales_kg__c = 0;
        if(WOCLastYearPEConversionValue > 0) 
            TW.PE.SOC_Last_year_PE_Sample_kg__c =WOCLastYearPEShipmentValue / WOCLastYearPEConversionValue ;
        else
            TW.PE.SOC_Last_year_PE_Sample_kg__c = 0;
        TW.PE.SOC_This_year_In_market_sales_kg__c = 0;
        TW.PE.SOC_This_year_PE_Sample_kg__c = 0;
        TW.PE.SOCInMarketValueHide__c = WOCThisYearInMarketSalesValue ;
        TW.PE.SOCConversionForcast__c = WOCThisYearPEConversionValue;            

        if(TotalLastYearInMarketSalesValue > 0) 
            TW.PE.SOC_Last_year_sales_weighting__c = WOCLastYearInMarketSalesValue / TotalLastYearInMarketSalesValue * 100;
        else
            TW.PE.SOC_Last_year_sales_weighting__c = 0;
        if(TotalLastYearPEConversionValue > 0) 
            TW.PE.SOC_Last_year_conversion_weighting__c = WOCLastYearPEConversionValue / TotalLastYearPEConversionValue * 100;
        else
            TW.PE.SOC_Last_year_conversion_weighting__c = 0;
        TableItemsDown.add(TW);   
        
        TableItemControl TT = new TableItemControl();
        TT.Str = '总计';
        TT.InputPercent = 100;
        TT.PE.SOC_Last_year_sales_weighting__c = 100;
        TT.PE.SOC_Last_year_conversion_weighting__c = 100;          
        if(TotalLastYearPEShipmentValue > 0) 
            TT.PE.SOC_Last_year_In_market_sales_kg__c = TotalLastYearInMarketSalesValue / TotalLastYearPEShipmentValue ;
        else
            TT.PE.SOC_Last_year_In_market_sales_kg__c = 0;
        if(TotalLastYearPEConversionValue > 0) 
            TT.PE.SOC_Last_year_PE_Sample_kg__c = TotalLastYearPEShipmentValue / TotalLastYearPEConversionValue ;
        else
            TT.PE.SOC_Last_year_PE_Sample_kg__c = 0;
        TT.PE.SOC_This_year_In_market_sales_kg__c = 0;
        TT.PE.SOC_This_year_PE_Sample_kg__c = 0;
        TT.PE.SOCInMarketValueHide__c = TotalThisYearInMarketSalesValue ;
        TT.PE.SOCConversionForcast__c = TotalThisYearPEConversionValue ;                      
        
        TableItemsDown.add(TT); 
                     
             
    }

    public void SetRecord(Boolean deal)
    {
        // set table items up
        PESM.Total_Budget_weighting__c = TableItemsUp[0].PE.Total_Budget_weighting__c;
        PESM.Total_Budget_allocation_kg__c = TableItemsUp[0].PE.Total_Budget_allocation_kg__c;
        if(TableItemsUp[0].PE.Total_Budget_allocation_kg__c == null)
            PESM.Total_Budget_Allocation_RMB__c = 0;
        else
            PESM.Total_Budget_Allocation_RMB__c = TableItemsUp[0].PE.Total_Budget_allocation_kg__c * PESM.Rate__c; 
        PESM.Budget_hold_in_HQ_weighting__c = TableItemsUp[1].PE.Total_Budget_weighting__c;
        PESM.Budget_hold_in_HQ_Budget_allocation_kg__c = TableItemsUp[1].PE.Total_Budget_allocation_kg__c;
        
        if(TableItemsUp[1].PE.Total_Budget_allocation_kg__c == null)
            PESM.Budget_hold_in_HQ_RMB__c = 0;
        else
            PESM.Budget_hold_in_HQ_RMB__c = TableItemsUp[1].PE.Total_Budget_allocation_kg__c * PESM.Rate__c; 
        PESM.Budget_auto_top_down_to_region_weighting__c = TableItemsUp[2].PE.Total_Budget_weighting__c;
        PESM.Budget_auto_top_down_to_region_Budget_kg__c = TableItemsUp[2].PE.Total_Budget_allocation_kg__c;
        if(TableItemsUp[2].PE.Total_Budget_allocation_kg__c == null)  
            PESM.Budget_auto_top_down_to_region_RMB__c = 0;
        else  
            PESM.Budget_auto_top_down_to_region_RMB__c = TableItemsUp[2].PE.Total_Budget_allocation_kg__c * PESM.Rate__c;

        //set table items down
        PESM.EOC_Last_year_sales_weighting__c = TableItemsDown[0].PE.SOC_Last_year_sales_weighting__c;
        PESM.EOC_Last_year_conversion_weighting__c = TableItemsDown[0].PE.SOC_Last_year_conversion_weighting__c;
        PESM.EOC_potential_score__c = TableItemsDown[0].PE.SOC_potential_score__c;
        PESM.EOC_total_score__c = TableItemsDown[0].PE.SOC_total_score__c;
        PESM.EOC_final_weighting__c = TableItemsDown[0].PE.SOC_final_weighting__c;        
        PESM.EOCInMarketValueHide__c = TableItemsDown[0].PE.SOCInMarketValueHide__c;
        PESM.EOCConversionForcast__c = TableItemsDown[0].PE.SOCConversionForcast__c;
        
        PESM.EOC_budget_allocation_kg__c = TableItemsDown[0].PE.SOC_budget_allocation_kg__c;
        

        PESM.EOC_Last_year_In_market_sales_kg__c = TableItemsDown[0].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.EOC_Last_year_PE_Sample_kg__c = TableItemsDown[0].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.EOC_budget_allocation_kg__c > 0)
            PESM.EOC_This_year_In_market_sales_kg__c = PESM.EOCInMarketValueHide__c / PESM.EOC_budget_allocation_kg__c;
        else
            PESM.EOC_This_year_In_market_sales_kg__c = 0;
        PESM.EOC_This_year_In_market_sales_kg__c = PESM.EOC_This_year_In_market_sales_kg__c.intValue();
        if((PESM.EOCConversionForcast__c > 0)&&(PESM.EOC_budget_allocation_kg__c > 0))
            PESM.EOC_This_year_PE_Sample_kg__c = PESM.EOC_budget_allocation_kg__c / PESM.EOCConversionForcast__c;
        else
            PESM.EOC_This_year_PE_Sample_kg__c = 0;    
        PESM.EOC_This_year_PE_Sample_kg__c = PESM.EOC_This_year_PE_Sample_kg__c.intValue();
        if(TableItemsDown[0].PE.SOC_budget_allocation_kg__c == null)
            PESM.EOC_Total_budget_RMB__c = 0;
        else
            PESM.EOC_Total_budget_RMB__c = TableItemsDown[0].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
        
                
        PESM.NOC_Last_year_sales_weighting__c = TableItemsDown[1].PE.SOC_Last_year_sales_weighting__c;
        PESM.NOC_Last_year_conversion_weighting__c = TableItemsDown[1].PE.SOC_Last_year_conversion_weighting__c;
        PESM.NOC_potential_score__c = TableItemsDown[1].PE.SOC_potential_score__c;
        PESM.NOC_total_score__c = TableItemsDown[1].PE.SOC_total_score__c;
        PESM.NOC_final_weighting__c = TableItemsDown[1].PE.SOC_final_weighting__c;
        PESM.NOCInMarketValueHide__c = TableItemsDown[1].PE.SOCInMarketValueHide__c;
        PESM.NOCConversionForcast__c = TableItemsDown[1].PE.SOCConversionForcast__c;        
        PESM.NOC_budget_allocation_kg__c = TableItemsDown[1].PE.SOC_budget_allocation_kg__c;
      
        PESM.NOC_Last_year_In_market_sales_kg__c = TableItemsDown[1].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.NOC_Last_year_PE_Sample_kg__c = TableItemsDown[1].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.NOC_budget_allocation_kg__c > 0)
            PESM.NOC_This_year_In_market_sales_kg__c = PESM.NOCInMarketValueHide__c / PESM.NOC_budget_allocation_kg__c;
        else
            PESM.NOC_This_year_In_market_sales_kg__c = 0;
        PESM.NOC_This_year_In_market_sales_kg__c = PESM.NOC_This_year_In_market_sales_kg__c.intValue();
        if(PESM.NOCConversionForcast__c > 0)
            PESM.NOC_This_year_PE_Sample_kg__c = PESM.NOC_budget_allocation_kg__c / PESM.NOCConversionForcast__c;
        else
            PESM.NOC_This_year_PE_Sample_kg__c = 0;
        PESM.NOC_This_year_PE_Sample_kg__c = PESM.NOC_This_year_PE_Sample_kg__c.intValue();
        if(TableItemsDown[1].PE.SOC_budget_allocation_kg__c  == null)
            PESM.NOC_Total_budget_RMB__c = 0;
        else
            PESM.NOC_Total_budget_RMB__c = TableItemsDown[1].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
       
        PESM.SOC_Last_year_sales_weighting__c = TableItemsDown[2].PE.SOC_Last_year_sales_weighting__c;
        PESM.SOC_Last_year_conversion_weighting__c = TableItemsDown[2].PE.SOC_Last_year_conversion_weighting__c;
        PESM.SOC_potential_score__c = TableItemsDown[2].PE.SOC_potential_score__c;
        PESM.SOC_total_score__c = TableItemsDown[2].PE.SOC_total_score__c;
        PESM.SOC_final_weighting__c = TableItemsDown[2].PE.SOC_final_weighting__c;
        PESM.SOCInMarketValueHide__c = TableItemsDown[2].PE.SOCInMarketValueHide__c;
        PESM.SOCConversionForcast__c = TableItemsDown[2].PE.SOCConversionForcast__c;         
        PESM.SOC_budget_allocation_kg__c = TableItemsDown[2].PE.SOC_budget_allocation_kg__c;
          
        PESM.SOC_Last_year_In_market_sales_kg__c = TableItemsDown[2].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.SOC_Last_year_PE_Sample_kg__c = TableItemsDown[2].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.SOC_budget_allocation_kg__c > 0)
            PESM.SOC_This_year_In_market_sales_kg__c = PESM.SOCInMarketValueHide__c / PESM.SOC_budget_allocation_kg__c;
        else
            PESM.SOC_This_year_In_market_sales_kg__c = 0;
        PESM.SOC_This_year_In_market_sales_kg__c = PESM.SOC_This_year_In_market_sales_kg__c.intValue();
        if((PESM.SOCConversionForcast__c > 0)&&(PESM.SOC_budget_allocation_kg__c > 0))
            PESM.SOC_This_year_PE_Sample_kg__c = PESM.SOC_budget_allocation_kg__c / PESM.SOCConversionForcast__c;
        else
            PESM.SOC_This_year_PE_Sample_kg__c = 0;
        PESM.SOC_This_year_PE_Sample_kg__c = PESM.SOC_This_year_PE_Sample_kg__c.intValue();
        if(TableItemsDown[2].PE.SOC_budget_allocation_kg__c == null)
            PESM.SOC_Total_budget_RMB__c = 0;
        else
            PESM.SOC_Total_budget_RMB__c = TableItemsDown[2].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
        
        PESM.WOC_Last_year_sales_weighting__c = TableItemsDown[3].PE.SOC_Last_year_sales_weighting__c;
        PESM.WOC_Last_year_conversion_weighting__c = TableItemsDown[3].PE.SOC_Last_year_conversion_weighting__c;
        PESM.WOC_potential_score__c = TableItemsDown[3].PE.SOC_potential_score__c;
        PESM.WOC_total_score__c = TableItemsDown[3].PE.SOC_total_score__c;
        PESM.WOC_final_weighting__c = TableItemsDown[3].PE.SOC_final_weighting__c;
        PESM.WOCInMarketValueHide__c = TableItemsDown[3].PE.SOCInMarketValueHide__c;
        PESM.WOCConversionForcast__c = TableItemsDown[3].PE.SOCConversionForcast__c;         
        PESM.WOC_budget_allocation_kg__c = TableItemsDown[3].PE.SOC_budget_allocation_kg__c;
        
        PESM.WOC_Last_year_In_market_sales_kg__c = TableItemsDown[3].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.WOC_Last_year_PE_Sample_kg__c = TableItemsDown[3].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.WOC_budget_allocation_kg__c > 0)
            PESM.WOC_This_year_In_market_sales_kg__c = PESM.WOCInMarketValueHide__c / PESM.WOC_budget_allocation_kg__c;
        else
            PESM.WOC_This_year_In_market_sales_kg__c = 0;
        PESM.WOC_This_year_In_market_sales_kg__c = PESM.WOC_This_year_In_market_sales_kg__c.intValue();
        if((PESM.WOCConversionForcast__c > 0)&&(PESM.WOC_budget_allocation_kg__c > 0))
            PESM.WOC_This_year_PE_Sample_kg__c = PESM.WOC_budget_allocation_kg__c / PESM.WOCConversionForcast__c;
        else
            PESM.WOC_This_year_PE_Sample_kg__c = 0;
        PESM.WOC_This_year_PE_Sample_kg__c = PESM.WOC_This_year_PE_Sample_kg__c.intValue();
        if(TableItemsDown[3].PE.SOC_budget_allocation_kg__c == null)
            PESM.WOC_Total_budget_RMB__c = 0;
        else
            PESM.WOC_Total_budget_RMB__c = TableItemsDown[3].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
     
        PESM.Deal__c = Deal;
        PESM.name = PESM.Year__c + '年' + '专业样品管理';
        insert PESM;
        
        if(TestServer)
            PESM.Details__c  =  'https://cs5.salesforce.com/'+PESM.id;
        else
            PESM.Details__c  =  'https://login.salesforce.com/'+PESM.id; 
        
        update PESM;
    
    }


    public PageReference Save()
    {
        Decimal AllocatedNum;

        for(TableItemControl t : TableItemsDown)
        {
           if(t.Str == '总计')
           {
               continue;
           }
            if(t.PE.SOC_budget_allocation_kg__c == null )
            {
                msg = '有大区未分配，请分配完成!';
                return null;
            }
        }
        
        AllocatedNum = TableItemsDown[0].PE.SOC_budget_allocation_kg__c + TableItemsDown[1].PE.SOC_budget_allocation_kg__c 
                        + TableItemsDown[2].PE.SOC_budget_allocation_kg__c + TableItemsDown[3].PE.SOC_budget_allocation_kg__c;
        
        if(AllocatedNum != TableItemsUp[2].PE.Total_Budget_allocation_kg__c)
        {
            msg = '分配公斤数应等于分配到大区的预算公斤数';
            return null;
        }

        SetRecord(false);
        
        
        UserRole SRMMM_SOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.SRMMM_SOC];
        User USER_SOC= [Select id From User Where UserRoleId =: SRMMM_SOC.id and IsActive =: true];
        UserRole SRMMM_EOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.SRMMM_EOC];
        User USER_EOC= [Select id From User Where UserRoleId =: SRMMM_EOC.id and IsActive =: true ];
        UserRole SRMMM_WOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.SRMMM_WOC];
        User USER_WOC= [Select id From User Where UserRoleId =: SRMMM_WOC.id and IsActive =: true];
        UserRole SRMMM_NOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.SRMMM_NOC];
        User USER_NOC= [Select id From User Where UserRoleId =: SRMMM_NOC.id and IsActive =: true];                        

        UserRole RGM_SOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleSOCRGM ];
        User RGM_USER_SOC= [Select id From User Where UserRoleId =: RGM_SOC.id and IsActive =: true];
        UserRole RGM_EOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleEOCRGM ];
        User RGM_USER_EOC= [Select id From User Where UserRoleId =: RGM_EOC.id and IsActive =: true];
        UserRole RGM_WOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleWOCRGM ];
        User RGM_USER_WOC= [Select id From User Where UserRoleId =: RGM_WOC.id and IsActive =: true];
        UserRole RGM_NOC = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNOCRGM ];
        User RGM_USER_NOC= [Select id From User Where UserRoleId =: RGM_NOC.id and IsActive =: true];    

        
        List<RegionalBudget__c> RegionalList = new List<RegionalBudget__c>(); 
        RegionalBudget__c RegionalSOC = new RegionalBudget__c(RGM__c = RGM_USER_SOC.id, Name = PESM.Year__c +'年华南区专业样品管理', PESampleManagement__c = PESM.id, SRMMM__c = USER_SOC.id);
        RegionalBudget__c RegionalEOC = new RegionalBudget__c(RGM__c = RGM_USER_EOC.id,Name = PESM.Year__c +'年华东区专业样品管理', PESampleManagement__c = PESM.id, SRMMM__c = USER_EOC.id);
        RegionalBudget__c RegionalWOC = new RegionalBudget__c(RGM__c = RGM_USER_WOC.id,Name = PESM.Year__c +'年华西区专业样品管理', PESampleManagement__c = PESM.id, SRMMM__c = USER_WOC.id);
        RegionalBudget__c RegionalNOC = new RegionalBudget__c(RGM__c = RGM_USER_NOC.id,Name = PESM.Year__c +'年华北区专业样品管理', PESampleManagement__c = PESM.id, SRMMM__c = USER_NOC.id);
        RegionalList.add(RegionalSOC);
        RegionalList.add(RegionalEOC);
        RegionalList.add(RegionalWOC);
        RegionalList.add(RegionalNOC);

        insert RegionalList;
        if(TestServer)
        {
            RegionalList[0].RecordUrl__c = 'https://cs5.salesforce.com/'+RegionalList[0].id;
            RegionalList[1].RecordUrl__c = 'https://cs5.salesforce.com/'+RegionalList[1].id;
            RegionalList[2].RecordUrl__c = 'https://cs5.salesforce.com/'+RegionalList[2].id;
            RegionalList[3].RecordUrl__c = 'https://cs5.salesforce.com/'+RegionalList[3].id;        
        }
        else
        {
            RegionalList[0].RecordUrl__c = 'https://login.salesforce.com/'+RegionalList[0].id;
            RegionalList[1].RecordUrl__c = 'https://login.salesforce.com/'+RegionalList[1].id;
            RegionalList[2].RecordUrl__c = 'https://login.salesforce.com/'+RegionalList[2].id;
            RegionalList[3].RecordUrl__c = 'https://login.salesforce.com/'+RegionalList[3].id;           
        }

        update RegionalList;
          
        PageReference pr=new PageReference('/'+PESM.id+'?retURL=%Fa'+ Prefix);
        pr.setRedirect(true);
        return pr;
    }

    public PageReference Deal()
    {
        SetRecord(true);
        
        PageReference pr=new PageReference('/'+PESM.id+'?retURL=%Fa'+ Prefix );
        pr.setRedirect(true);
        return pr;
    }
            
    public PageReference Cancel()
    {
        PageReference pr=new PageReference('/'+Prefix );
        pr.setRedirect(true);
        return pr;
    }
    private final ApexPages.StandardController controller;
    
    static testMethod void OverrideViewTest()
    {
        Profile p  = [select id from profile where name = 'System Administrator'or name =: '系统管理员'];
        
        User u1 = new User (alias = 'standt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='asdfdaa@testorg1.com');
        insert u1;
        
        System.runAs(u1) {
            PE_Sample_Management__c PE = new PE_Sample_Management__c();
            PE.ownerid = u1.id;
            ApexPages.StandardController AP=new ApexPages.StandardController(PE );
            OverridePESampleNew testCase = new OverridePESampleNew(AP);
            testCase.PESM = new PE_Sample_Management__c();
            

            testCase.Save();
            testCase.Cancel();
            testCase.Deal();
        
        }    
    }
 
}